$(function(){
	/**调用初始化方法*/
	queryRole();
});
/*function getAllRole(){
	$.post("/GetAllGoods/",null,function(str){
		str = decodeURI(str);
		var data = $.parseJSON(str);
		for(var i=0;i<data.length;i++){
			$("#selectId").append("<option value='"+data[i].RoleId+"'>"+data[i].RoleName+"</option>");
		}
	});
}*/

/*function queryForm(){
	var userName = $("#queryName").val();
	var roleId = $("#querySelect").val();
	queryRole(userName,roleId);
}*/

function queryRole(){
	$(this).hide();
	$('#usertable').bootstrapTable('destroy');
	$('#usertable').bootstrapTable({
		method: 'get',
		url: '/GetAllChangeGoods/',
		cache: false,
		striped: true,
		pagination:true,
		pageSize:20,
		pageList: [5, 10, 20,50],
		pageNumber:1,
		sidePagination:'server',
		width:'600',
		columns: [
			{
				field: 'id',
				title: 'Item ID',
				align: 'right',
				valign: 'bottom',
				visible:false
			},{
				field: 'goodsname',
				title: '商品名称',
				align: 'center',
				formatter: turnAround,
				valign: 'middle'

			}, {
				field: 'coins',
				title: '金币兑换数',
				formatter: turnAround,
				align: 'center',
				valign: 'middle'
			},{
				field: 'goodsUrl',
				title: '图片地址',
				formatter: turnAround,
				align: 'center',
				valign: 'middle'


			},{
				field: 'typeid',
				title: '商品类型',
				align: 'center',
				valign: 'middle',
				visible:false

			},{
				field: 'state',
				title: '商品状态',
				align: 'center',
				valign: 'middle'

			},{
				field: 'operate',
				title: '操作',
				align: 'center',
				valign: 'middle',
				formatter: operateFormatter,
				events: operateEvents
			}]
	});
}
/**
 * table图标区
 */
function operateFormatter(value, row, index) {
	if(row.state=="下架商品"){
		return [
			'<button class="updateUser" id="updateUser">修改</button>',
			'<button class="upGoods" id="up">上架</button>'
			].join('');
	}else{
		return [
			'<button class="updateUser" id="updateUser">修改</button>',
			'<button class="upGoods" id="down">下架</button>'
			].join('');
	}
}

/**
 * 图标绑定点击事件
 */
window.operateEvents = {
		'click .updateUser': function (e, value, row, index) {
			isModify=1;
			$("#titleId").html("修改用户");
			$(".com_title").val(decodeURI(row.goodsname,"utf-8"));
			$(".price").val(decodeURI(row.coins,"utf-8"));
			if(row.state=="下架商品"){
				$(".state").val("1");
			}else{
				$(".state").val("0");
			}
			$(".show_img_url").val(decodeURI(row.goodsUrl,"utf-8"));
			$("#ids").val(row.id);
			$("#show_div_user").show();
		},
		'click .upGoods': function (e, value, row, index) {
			var id=row.id;
			var state=1;
			if(row.state=="下架商品"){
				state=0;
			}
			if(confirm("你确定提交该操作吗？")){
				$.get("/changeGoodsSailOn/?id="+id+"&state="+state,null,function(data){
					alert(data);
					queryRole();
				});
			}
		}
};

function turnAround(value, row, index){
	return decodeURI(value,"utf-8"); 
}



/* 点击提交的事件  */
$("#addUser").click(function(){
	$(".show_div").hide();
	var data={};
	var goodsname=$(".com_title").val();
	var coins=$(".price").val();
	var state=$(".state").val();
	var goodsUrl=$(".show_img_url").val();
	var typeid=$(".type").val();
	data.goodsname = goodsname;
	data.coins = coins;
	data.state = state;
	data.goodsUrl = goodsUrl;
	data.typeid = typeid;
	var userid = $("#ids").val();
	alert(userid);
	if(userid!=0 && userid!=null){
		data.id = userid;
	}
	if(goodsname&&coins&&state&&goodsUrl&&typeid){
		if(confirm("你确定提交该操作吗？")){
			$(this).html('发送中...');
			$(this).css({"background":"#616161"});
			$.get("/addOrUpdateChangeGoods/",data,function(data){
				data = decodeURI(data);
				var data1 = $.parseJSON(data);
				if(data1.state == 1){
					window.location.reload();
					alert(data1.message);
				}else{
					$(".showList_submit").html('提交');
					$(".showList_submit").css({"background":"#438EB9"});
					alert(data1.message);
					$("#show_div_user").show();
				}
			});
		}
	}else{
		alert("填写错误");
	}
});
